$(document).ready(function() {	
	$("#frmEdit").validate({
		errorClass		: 'errorMessage',
		errorElement	: 'span',
		onfocusout		: false,
		onkeyup			: false,
		rules: {	
			pcategoryid: {
				required			: true
			},
			ptype_chk: {
				requiredType		: true
			},
			pname: {
				required			: true,
				minlength			: 5
			},
			pcontent: {
				requiredContent		: true,
				minlengthContent	: true
			},
			pcolor_chk: {
				requiredColor		: true
			},
			pgender: {
				requiredGender		: true
			},
			pimage: {				
				validFormat			: true,
				validSize			: true
			},
			pthumb: {
				requiredThumb		: true,
				validFormatThumb	: true,
				validSizeThumb		: true
			},
			psize_chk: {
				requiredSize		: true
			}
		},
		messages: {	
			pcategoryid: {
				required			: category_required
			},
			ptype_chk: {
				requiredType		: type_required
			},
			pname: {
				required			: name_required,
				minlength			: name_minlength
			},
			pcontent: {
				requiredContent		: content_required,
				minlengthContent	: content_minlength
			},
			pcolor_chk: {
				requiredColor		: color_required
			},
			pgender: {
				requiredGender		: gender_required
			},
			pimage: {				
				validFormat			: image_format,
				validSize			: image_size
			},
			pthumb: {
				requiredThumb		: thumb_required,
				validFormatThumb	: thumb_format,
				validSizeThumb		: thumb_size
			},
			psize_chk: {
				requiredSize		: size_required
			}
		},
		errorPlacement: function(label, element) {			
			var id = element.attr('id');
			if (id == 'ptype_chk')	{
				label.insertAfter($('#divType'));
			}
			else if (id == 'pcontent')	{
				label.insertAfter($('#pcontent___Frame'));
			}
			else if (id == 'pcolor_chk')	{
				label.insertAfter($('#divColor'));
			} 
			else if (id == 'psize_chk') {				
				label.insertAfter($('#divSizeMan'));
			}
			else {
				label.insertAfter($('#' + id));			
			}
		},
		submitHandler: function(form) {				
			form.submit();
		}
	});
	$.validator.addMethod('requiredType',function(){
		var intCheck = $("input[name='ptype[]']:checked:enabled").length;	
		if(intCheck == 0) return false;
		return true;											  
	});
	$.validator.addMethod('requiredContent',function(){
		var oEditorContent 	= FCKeditorAPI.GetInstance('pcontent') ;
		var strContent		= stripTags(oEditorContent.GetXHTML(true), 1);		
		if(strContent == '')
			return false;		
		return true;												 
	});
	$.validator.addMethod('minlengthContent',function(){
		var oEditorContent 	= FCKeditorAPI.GetInstance('pcontent') ;
		var strContent		= stripTags(oEditorContent.GetXHTML(true), 1);		
		if(strContent != '' && strContent.length < 20)
			return false;		
		return true;												 
	});
	$.validator.addMethod('requiredColor',function(){
		var intCheck = $("input[name='pcolor[]']:checked:enabled").length;	
		if(intCheck == 0) return false;
		return true;											  
	});
	$.validator.addMethod('requiredGender',function(){
		var strImage = $('#pimage').val();
		var intGender = $('#pgender').val();		
		if(strImage != "" && intGender == "") {
			return false;
		}
		return true;
	});
	$.validator.addMethod('validFormat',function(){
		var strImage = $('#pimage').val();	
		if(strImage != '')
			return validateImage(strImage);
		return true;
	});
	$.validator.addMethod('validSize',function(){	
		var strImage = $('#pimage').val();
		if(strImage != '') {
			if($.browser.msie)
			{
				var objFSO = new ActiveXObject("Scripting.FileSystemObject");
				var sPath = $("#pimage")[0].value;
				var objFile = objFSO.getFile(sPath);
				var iSize = objFile.size;
				iSize = iSize/ 1024;
			}
			else {
				iSize = ($("#pimage")[0].files[0].size / 1024); 			
			}
			
			if((iSize / 1024) > 1) {
				return false;	
			}
		}
		return true;
	});
	$.validator.addMethod('requiredThumb',function(){
		var strImage = $('#pimage').val();
		var strThumb = $('#pthumb').val();
		if(strImage != "" && strThumb == "") {
			return false;
		}
		return true;
	});
	$.validator.addMethod('validFormatImage',function(){
		var strImage = $('#pthumb').val();	
		if(strImage != '')
			return validateImage(strImage);
		return true;
	});
	$.validator.addMethod('validSizeImage',function(){	
		var strImage = $('#pthumb').val();
		if(strImage != '') {
			if($.browser.msie)
			{
				var objFSO = new ActiveXObject("Scripting.FileSystemObject");
				var sPath = $("#pthumb")[0].value;
				var objFile = objFSO.getFile(sPath);
				var iSize = objFile.size;
				iSize = iSize/ 1024;
			}
			else {
				iSize = ($("#pthumb")[0].files[0].size / 1024); 			
			}
			
			if((iSize / 1024) > 1) {
				return false;	
			}
		}
		return true;
	});
});